﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.Threading;
using System.Globalization;

using System.Data;

/// <summary>
/// MyObj 的摘要描述
/// </summary>
public class MyObj
{
    public MyObj()
    {
        //
        // TODO: 在此加入建構函式的程式碼
        //
    }

    public static CultureInfo CurrentCulture
    {
        get
        {
            return Thread.CurrentThread.CurrentUICulture;
        }
    }

    public static void DownloadFile(string output_name, string path)
    {
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(output_name));
        HttpContext.Current.Response.WriteFile(path);
        HttpContext.Current.Response.End();
    }

    public static void DownloadFile(string output_name, byte[] b)
    {
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(output_name));
        HttpContext.Current.Response.BinaryWrite(b);
        HttpContext.Current.Response.End();
    }

    public static string JoinFormat(string separator, string format, IEnumerable list)
    {
        return JoinFormat(separator, format, list, string.Empty);
    }

    public static string JoinFormat(string separator, string format, IEnumerable list, string col)
    {
        var tmp = new List<string>();

        int i = 0;

        foreach (var item in list)
        {
            if (list is DataView)
            {
                tmp.Add(string.Format(format, (item as DataRowView)[col]));
            }
            else
            {
                tmp.Add(string.Format(format, item.ToString()));
            }
            i++;
        }

        if (i == 0)
        {
            tmp.Add(string.Format(format, string.Empty));
        }

        return string.Join(separator, tmp);
    }

    public static void ShowMessage(string classKey, string resourceKey)
    {
        var message = HttpContext.GetGlobalResourceObject(classKey, resourceKey).ToString();

        ShowMessage(message);
    }

    public static void ShowMessage(string message)
    {
        //ScriptManager.GetCurrent(HttpContext.Current.Handler as Page).re

        // convert to unicode rep. string
        var sk = new Stack<byte>();
        var result = string.Empty;

        foreach (byte b in Encoding.Unicode.GetBytes(message))
        {
            sk.Push(b);

            if (sk.Count == 2)
            {
                byte b1 = sk.Pop();
                byte b2 = sk.Pop();

                result += string.Format(@"\u{0:x2}{1:x2}", b1, b2);
            }
        }
        //

        var p = HttpContext.Current.Handler as Page;
        var script = string.Format("alert('{0}');", result);
        
        ScriptManager.RegisterClientScriptBlock(p, p.GetType(), p.GetType().ToString(), script, true);
    }
}